<template>
    <div class="container">
        <div class="tabCommonTitle">
            <span>申报单位意见</span>
        </div>
        <div class="tableForm">
            <table style="text-align:center;" width="1110" border="" cellspacing="0" align="center" cellpadding="">
                <tr>
                    <td width="180">申报单位：</td>
                    <td colspan="3">
                        <!-- <a-input v-model="sbdw"/> -->
                        <span>{{sbdw}}</span>
                    </td>
                </tr>
                <tr>
                    <td width="180">成果名称：</td>
                    <td colspan="3">
                        <!-- <a-input v-model="resultInfo.cxj_mc_zw"/> -->
                        <span>{{resultInfo.cxj_mc_zw}}</span>
                    </td>
                </tr>
                <tr>
                    <td width="180">成果申报类别 ：</td>
                    <td width="350">
                        <!-- <a-input v-model="resultInfo.fl_mc"/> -->
                        <span>{{resultInfo.fl_mc}}</span>
                    </td>
                    <td width="240">项目来源：</td>
                    <td width="340">
                        <!-- <a-input v-model="resultInfo.cgly_mc"/> -->
                        <span>{{resultInfo.cgly_mc}}</span>
                    </td>
                </tr>
                <tr>
                    <td width="180"><i>*</i>声明：</td>
                    <td colspan="3">
                        <!-- <a-textarea v-model="dwsmSm" :auto-size="{ minRows: 3 }" /> -->
                        <span>{{dwsmSm}}</span>
                    </td>
                </tr>
                <tr>
                    <td width="180"><i>*</i>附件<em style="display:block;color: #CB0C0C;font-style: normal;font-size: 14px;">(请上传pdf格式，50M以内)</em></td>
                    <td width="350">
                        <!-- <a-input v-model="fileInfo.fileName"/> -->
                        <span>{{fileInfo.fileName}}</span>
                    </td>
                    <td colspan="2" align="left" style="padding:10px 0">
                        <a-button class="templateDownload" style="margin:0 20px;" :disabled="fileInfo.id?false:true" @click="viewFile">查看</a-button>
                        <a-button class="templateDownload" style="margin-right:20px;" :disabled="resultInfo.sbztbh!='100'?false:true" @click="downFile">模板下载</a-button>
                        <a-upload
                            accept=".pdf"
                            name="file"
                            class="unitIdea"
                            :customRequest="customRequest"
                            :beforeUpload="beforeUpload"
                            :showUploadList="{showRemoveIcon:false}"
                            :fileList="fileList"
                            >
                            <a-button :disabled="resultInfo.sbztbh!='100'?false:true" type="primary">上传</a-button>
                        </a-upload>
                        <p><em style="display:block;color: #CB0C0C;font-style: normal;font-size: 14px;margin-top:4px;padding-left: 6px;">请材料确认后，点击‘模板下载’下载申报单位意见表，并打印盖章后扫描为pdf文件，上传系统中</em></p>
                        <p><em style="display:block;color: #CB0C0C;font-style: normal;font-size: 14px;margin-top:4px;padding-left: 6px;">成果完成人、完成单位须填写正确且与系统填报内容保持一致，该信息将作为颁发证书依据，请仔细核对。</em></p>
                    </td>
                </tr>
            </table>
            <!-- <div class="submit_btn pointer" :class="is_modify=='Y'?'':'disabled'" @click="saveUnitIdea">保存</div> -->
            <!-- <a-button class="save_btn pointer" type="primary" :class="is_modify=='Y'?'':'disabled'" :disabled="is_modify=='Y'?false:true" @click="saveUnitIdea">保存</a-button> -->
            <a-button class="save_btn pointer" type="primary" :class="{'disabled': is_modify == 'N' && resultInfo.sbztbh=='102' }" :disabled="savedisabled" @click="saveUnitIdea">保存</a-button>
        </div>

        <!-- <div class="showpdf" v-if="showpdf">
            <img class="closebtn pointer" @click="closepdf" src="@/assets/img/close1.png"/>
            <testPdf :cxj_cxj_id="cxj_cxj_id" :file-path="filePath"></testPdf>
        </div> -->
        <testPdf :cxj_cxj_id="cxj_cxj_id" :file-path="filePath" :showPdf="showpdf"></testPdf>
    </div>
</template>

<script>
import {
    // 创新奖申报-下载单位意见模板
    cxjsbDownloadDwyjmb,
    // 创新奖申报-单位意见保存和修改
    cxjsbDwyjAdd,
    // 创新奖申报-单位意见详情
    cxjsbGetSbbdwyj,
    // 创新奖申报-单位意见声明
    cxjsbGetDwyjsm,
    // 成果基本薪详情
    cxjsbGetCxjByid,
    // 获取申报单位
    cxjsbSbtjdwlist,
    // 项目来源（成果来源）
    cxjsbCglylist
} from "@/api/shenbao";
import testPdf from '@/views/declarationSystem/declare/testPdf';
function getBase64(img, callback) {
  const reader = new FileReader();
  reader.addEventListener('load', () => callback(reader.result));
  reader.readAsDataURL(img);
}
import { mapActions } from 'vuex'
export default {
    name: 'UnitIdea',
    props: ['is_modify'],
    data(){
        return {
            detailInfo: {},
            resultInfo: {},
            dwsmSm: '',
            sblb: '',
            sbdw: '',
            fileInfo: {},
            cglylist: [],
            xmly: '',
            file: '',
            fileList:[],
            showpdf: false,
            cxj_cxj_id: '',
            filePath: '',  
            isdisabled: true,
            savedisabled: false,
        }
    },
    components: {
        testPdf
    },
    mounted(){
        this.cxj_cxj_id = this.$route.query.cxj_cxj_id;
        this.getCxjsbSbtjdwlist();
        this.getCxjsbGetDwyjsm();
        this.getCxjsbGetCxjByid();
        this.getDetail();
    },
    methods: {
        ...mapActions('fillingSteps', ['getStatus']),
        customRequest(file){
            let fileList = [...this.fileList, file.file]
            this.fileList = fileList.slice(-1);
            this.file = this.fileList[0];
            return false;
        },
        beforeUpload(file){
            const isLt50M = file.size / 1024 / 1024 < 50;
            if (!isLt50M) {
              this.$message.error('文件大小不能超过50MB!');
            }else if(file.type!='application/pdf'){
              this.$message.error('请上传pdf格式的文件');
            }
        },
        saveUnitIdea(){
            let that = this;
            let formData = new FormData();
            // 完成人id
            if (that.fileInfo.id) {
                formData.append('id', that.fileInfo.id);
            } else {
                formData.append('id', '');
            }
            // 姓名
            formData.append('cxj_cxj_id', that.$route.query.cxj_cxj_id)
            formData.append('file', that.file)
            that.savedisabled = true;
            cxjsbDwyjAdd(formData).then(res=>{
                let data = res.data;
                if (data.success) {
                    that.$message.success('上传成功');
                    that.getStatus(that.$route.query.cxj_cxj_id)
                    that.getDetail();
                    that.savedisabled = false;
                } else {
                    that.$message.error(data.message)
                    that.savedisabled = false;
                }
            })
        },
        // closepdf() {
        //     this.filePath = '';
        //     this.showpdf = false;
        // },
        viewFile(){
            if (this.fileInfo && this.fileInfo.filePath) {
                // this.showpdf = true;
                this.showpdf = !this.showpdf;
                this.filePath = this.fileInfo.filePath;
            } else {
                this.$message.error('请上传附件')
            }
            /* if (this.fileInfo && this.fileInfo.filePath) {
                let routeData = this.$router.resolve({
                    path: '/shenbao/pdfFile',
                    query: {
                        cxj_cxj_id: this.$route.query.cxj_cxj_id,
                        path: this.fileInfo.filePath
                    }
                });
                window.open(routeData.href, '_blank');
            } else {
                this.$message.error('请上传附件')
            } */
        },
        /* downFile() {
            let that = this;
            let data = {
                cxj_cxj_id: that.$route.query.cxj_cxj_id
            }
            // 1. 创建一个 xhr 对象
            var xhr = new XMLHttpRequest();
            // 2. 设置请求的方式和路径
            xhr.open('GET', 'http://211.160.78.162:8080/jeecg-boot/cxj/zgSbSbbDwyj/cxjsb_downloadDwyjmb?cxj_cxj_id='+that.$route.query.cxj_cxj_id, true);
            // 3. 发送请求
            xhr.send(null);
            // 4. 注册事件
            xhr.onload = function () {
                // 通过 xhr 的 responseText 获取到响应的响应体
                console.log(this.responseText)
            }
        }, */
        saveAs (obj, fileName) {
            let ele = document.createElement('a');
            ele.download = fileName || '下载';
            ele.href = URL.createObjectURL(obj); // 绑定a标签
            ele.style.display = 'none';
            document.body.appendChild(ele); // 兼容火狐浏览器
            ele.click();
            setTimeout(function () { // 延时释放
                URL.revokeObjectURL(obj); // 用URL.revokeObjectURL()来释放这个object URL
                document.body.removeChild(ele);// 兼容火狐浏览器
            }, 100);
        },
        downFile() {
            let that = this;
            let data = {
                cxj_cxj_id: that.$route.query.cxj_cxj_id
            }
            cxjsbDownloadDwyjmb(data).then(res=>{
                this.downfn("单位意见", res)
            })
        },
        downfn(title, res) {
            console.log(title, data)
            const data = res.data;
            const url = window.URL.createObjectURL(new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}))
            const link = document.createElement('a')
            link.style.display = 'none'
            link.href = url;
            link.setAttribute('download', title+'.pdf')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link)
        },
        getDetail(){
            this.fileList = [];
            cxjsbGetSbbdwyj({
                cxj_cxj_id: this.$route.query.cxj_cxj_id
            }).then(res=>{
                let data = res.data;
                if (data.success) {
                    if (data.result) {
                        this.fileInfo = data.result;
                        this.fileList.push({  
                            uid: data.result.id,    // 文件唯一标识，建议设置为负数，防止和内部产生的 id 冲突  
                            name: data.result.fileName,   // 文件名  
                            status: 'done', // 状态有：uploading done error removed  
                            response: '{"status": "success"}', // 服务端响应内容  
                        })
                    }
                }
            })
        },
        // 单位意见声明
        getCxjsbGetDwyjsm(){
            cxjsbGetDwyjsm({
                cxj_cxj_id: this.$route.query.cxj_cxj_id
            }).then(res=>{
                let data = res.data;
                if (data.success) {
                    this.dwsmSm = data.result.dwsmSm;
                }
            })
        },
        // 获取申报单位
        getCxjsbSbtjdwlist(){
            cxjsbSbtjdwlist({
                cxj_cxj_id: this.$route.query.cxj_cxj_id
            }).then(res=>{
                let data = res.data;
                if (data.success){
                    if(data.result.length){
                        this.sbdw = data.result[0].sbdw_qymc;
                    }
                }
            })
        },
        // 获取成果信息
        getCxjsbGetCxjByid(){
            cxjsbGetCxjByid({
                cxj_cxj_id: this.$route.query.cxj_cxj_id
            }).then(res=>{
                let data = res.data;
                if (data.success) {
                    this.resultInfo = data.result;
                    if (this.is_modify == 'N') {
                        if (this.resultInfo.sbztbh=='101') {
                            this.isdisabled = false;
                            this.savedisabled = false;
                        } else {
                            this.isdisabled = true;
                            this.savedisabled = true;
                        }
                    } else {
                        this.isdisabled = true;
                        this.savedisabled = false;
                    }
                    // is_modify=='Y'?resultInfo.sbztbh=='101'?false:true:true
                }
            })
        }
    }
}
</script>
<style lang="less">
.unitIdea {
    .ant-upload-list {
        margin-left: 20px;
    }
}
</style>
<style lang="less" scoped>
.tableForm {
    margin-top: 11px;
    table {
        border: 1px solid #ECEBEB;
        tr {
            height: 40px;
            td {
                text-align: left;
                color: #333333;
                font-size: 14px;
                font-weight: 400;
                padding-left: 10px;
                i {
                    color: #CB0C0C;
                    margin-right: 5px;
                }
                input {
                    width: 100%;
                    height: 100%;
                    border: 0;
                    outline: 0;
                }
                textarea {
                    border:none;
                }
            }
        }
    }
}
.chakan {
    width: 70px;
    height: 32px;
    border: 1px solid #D9D9D9;
    line-height: 32px;
    color:#333;
    text-align: center;
    display: inline-block;
    margin-left: 20px;
    border-radius: 4px;
}
.shangchuan {
    width: 70px;
    height: 32px;
    line-height: 32px;
    color:#fff;
    text-align: center;
    border: 1px solid #3388EE;
    background: #3388EE;
    margin-left: 10px;
    border-radius: 4px;
    display: inline-block;
}
.save_btn {
    width: 250px;
    height: 60px;
    border-radius: 4px;
    text-align: center;
    line-height: 60px;
    font-size: 22px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #FFFFFF;
    display: block;
    margin: 40px auto 0;
}

</style>
